package com.inc.admin.service.biz.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inc.admin.dao.biz.BookDao;
import com.inc.admin.dao.biz.BookHistoryMapper;
import com.inc.admin.domain.biz.Book;
import com.inc.admin.domain.biz.BookHistory;
import com.inc.admin.service.biz.BookHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @author admin
* @description 针对表【book_history】的数据库操作Service实现
* @createDate 2024-11-04 22:28:16
*/
@Service
public class BookHistoryServiceImpl implements BookHistoryService{
    @Resource
    private BookHistoryMapper bookHistoryMapper;
    @Override
    public Object listHistoryByPage(BookHistory req) {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<BookHistory> list = bookHistoryMapper.listHistoryByPage();
        return new PageInfo<>(list);
    }

    @Override
    public int insertHistory(BookHistory req) {
        int result = 0;
        //查询图书库存，判断库存是否足够
        Book book = bookHistoryMapper.selectBookById(req.getBookId());
        if(book.getCount()==0|| book.getCount()<req.getCount()){
            result = -1;
        }else{
            req.setState(0);
            result = bookHistoryMapper.insertHistory(req);
            //修改图书库存
            bookHistoryMapper.updateBookCount(req.getBookId(),book.getCount()-req.getCount());
        }

        return result;
    }

    @Override
    public int updateHistory(BookHistory req) {
        int result = bookHistoryMapper.updateHistory(req);
        if(Objects.nonNull(req.getBackTime())){
            bookHistoryMapper.updateBookBackCount(req.getBookId(),req.getCount());
        }

        return result;
    }

    @Override
    public int deleteHistory(Integer id) {
        int result = bookHistoryMapper.deleteHistory(id);
        return result;
    }
}
